<?php

namespace App\Jobs;

use App\Exceptions\Arr;
use App\Models\Appoint;
use App\Models\AppointNum;
use App\Models\HospitalUserFamily;
use App\Models\InterviewRemind;
use App\Models\Notice;
use App\Models\TempPushRecord;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class InterviewRemindJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $ids;
    public $tries = 0;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($ids)
    {
        $this->ids = $ids;
        $this->onQueue('interview_remind');
        $this->onConnection('redis');
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $ids = $this->ids;
        $InterviewReminds = InterviewRemind::query()
            ->whereIn('id', $ids)
            ->where('status',0)->get();
        if ($InterviewReminds->isNotEmpty()) {
            //TempPushRecord::upsert($data,['user_id','temp_id']);

            foreach ($InterviewReminds as $InterviewRemind) {
                $tempPushRecord = TempPushRecord::createTemp($InterviewRemind->user_id, 4);

                try {
                    TempPush::dispatch($tempPushRecord, $InterviewRemind);
                    $InterviewRemind->status = 2;
                    $InterviewRemind->save();
                } catch (\Exception $e) {
                    $InterviewRemind->status = 3;
                    $InterviewRemind->save();
                    $this->fail($e->getMessage());
                }
            }
        }else{
            $this->fail('数据为空id全部不存在');
        }
    }
}
